package com.jf.cloud.user.controller.app;

import com.jf.cloud.api.auth.feign.AuthSocialFeignClient;
import com.jf.cloud.user.model.UserTravelsComment;
import com.jf.cloud.user.model.UserTravelsCommentLog;
import com.jf.cloud.user.service.UserService;
import com.jf.cloud.user.service.UserTravelsCommentLogService;
import com.jf.cloud.user.service.UserTravelsCommentService;
import com.jf.cloud.user.vo.UserTravelsCommentVO;
import com.jf.cloud.user.dto.UserTravelsCommentDTO;

import com.jf.cloud.api.auth.bo.UserInfoInTokenBO;
import com.jf.cloud.common.database.dto.PageDTO;
import com.jf.cloud.common.database.vo.PageVO;
import com.jf.cloud.common.response.ServerResponseEntity;
import com.jf.cloud.common.security.AuthUserContext;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import ma.glasnost.orika.MapperFacade;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.List;

/**
 * 
 * 游记评论列表
 * @author zz
 * @date 2023-11-10 16:10:35
 */
@RestController("appUserTravelsCommentController")
@RequestMapping("/user_travels_comment")
@Tag(name = "游记评论")
public class UserTravelsCommentController {

    @Autowired
    private UserTravelsCommentService userTravelsCommentService;

    @Autowired
    private UserTravelsCommentLogService userTravelsCommentLogService;

    @Autowired
    private UserService userService;

    @Autowired
    private MapperFacade mapperFacade;

    @GetMapping("/page")
    @Operation(summary = "分页获取评论列表", description = "分页获取评论列表")
    public ServerResponseEntity<PageVO<UserTravelsComment>> page(@Valid PageDTO pageDTO, @ParameterObject UserTravelsCommentDTO userTravelsCommentDTO) {
        if(userTravelsCommentDTO.getTravelsId()==null){
            return ServerResponseEntity.showFailMsg("记录ID不能为空");
        }
        PageVO<UserTravelsComment> userTravelsCommentPage = userTravelsCommentService.page(pageDTO,userTravelsCommentDTO);
        List<UserTravelsComment> list=userTravelsCommentPage.getList();
        for (int i = 0; i < list.size(); i++) {
            UserTravelsComment userTravelsComment=list.get(i);
            userTravelsComment.setNickName(userService.getById(userTravelsComment.getUserId()).getNickName());
            userTravelsComment.setImgUrl(userService.getById(userTravelsComment.getUserId()).getPic());
            userTravelsComment.setCount(userTravelsCommentLogService.count(userTravelsComment.getId()));
        }
        return ServerResponseEntity.success(userTravelsCommentPage);
    }

    @GetMapping
    @Operation(summary = "获取", description = "根据id获取")
    public ServerResponseEntity<UserTravelsComment> getById(@RequestParam Long id) {
        return ServerResponseEntity.success(userTravelsCommentService.getById(id));
    }

    @PostMapping
    @Operation(summary = "保存", description = "保存")
    public ServerResponseEntity<Void> save(@Valid @RequestBody UserTravelsCommentDTO userTravelsCommentDTO) {
        if(userTravelsCommentDTO.getTravelsId()==null){
            return ServerResponseEntity.showFailMsg("记录ID不能为空");
        }
        Long userId = AuthUserContext.get().getUserId();
        userTravelsCommentDTO.setUserId(userId);
        UserTravelsComment userTravelsComment = mapperFacade.map(userTravelsCommentDTO, UserTravelsComment.class);
        userTravelsCommentService.save(userTravelsComment);
        return ServerResponseEntity.success();
    }

    @PutMapping
    @Operation(summary = "更新", description = "更新")
    public ServerResponseEntity<Void> update(@Valid @RequestBody UserTravelsCommentDTO userTravelsCommentDTO) {
        if(userTravelsCommentDTO.getTravelsId()==null){
            return ServerResponseEntity.showFailMsg("记录ID不能为空");
        }
        Long userId = AuthUserContext.get().getUserId();
        userTravelsCommentDTO.setUserId(userId);
        UserTravelsComment userTravelsComment = mapperFacade.map(userTravelsCommentDTO, UserTravelsComment.class);
        userTravelsCommentService.update(userTravelsComment);
        return ServerResponseEntity.success();
    }

    @DeleteMapping
    @Operation(summary = "删除", description = "根据id删除")
    public ServerResponseEntity<Void> delete(@RequestParam Long id) {
        userTravelsCommentService.deleteById(id);
        return ServerResponseEntity.success();
    }
}
